package com.yaorange.jk.web.cargo;

import com.opensymphony.xwork2.ModelDriven;
import com.yaorange.jk.entity.ContractProduct;
import com.yaorange.jk.entity.Factory;
import com.yaorange.jk.service.ContractProductService;
import com.yaorange.jk.service.FactoryService;
import com.yaorange.jk.utils.Pagination;
import com.yaorange.jk.web.BaseAction;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author coach tam
 * @date 2017/11/1
 */
public class ContractProductAction extends BaseAction implements ModelDriven<ContractProduct> {


    private ContractProduct model = new ContractProduct();

    //分页对象
    private Pagination page = new Pagination();

    @Autowired
    private ContractProductService contractProductService;

    @Autowired
    private FactoryService factoryService;

    private List<Factory> factoryList;



    /**
     * 跳转添加页面
     */
    public String tocreate() {
        factoryList = factoryService.findListByType("货物");
        //分页对象
        page = contractProductService.findPage(page.getPageNo(), page.getPageSize(),model.getContract().getId());
        page.setUrl("contractProductAction_tocreate");
        push(page);
        return "tocreate";
    }

    /**
     * 添加
     */
    public String insert() {
        contractProductService.save(model);
        return tocreate();
    }

    /**
     * 跳转更新页面
     * @return
     */
    public String toupdate() {
        factoryList = factoryService.findListByType("货物");
        //根据id查询老数据
        model = contractProductService.findById(model.getId());
        push(model);
        return "toupdate";
    }

    /**
     *更新
     */
    public String update() {
        contractProductService.update(model);
        return tocreate();
    }

    /**
     * 批量删除
     */
    public String delete() {
        String[] ids = model.getId().split(", ");
        contractProductService.deleteByIds(ids);
        return tocreate();
    }
    public void setPage(Pagination page) {
        this.page = page;
    }

    @Override
    public ContractProduct getModel() {
        return model;
    }


    public List<Factory> getFactoryList() {
        return factoryList;
    }




}
